package com.liit.controller.func;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.annotation.SystemLog;
import com.liit.controller.index.BaseController;
import com.liit.entity.WeixinUserFormMap;
import com.liit.entity.WxuserGroupFormMap;
import com.liit.exception.SystemException;
import com.liit.mapper.WxuserGroupMapper;
import com.liit.plugin.PageView;
import com.liit.util.Common;

/**
 * @author dingweibo
 * @Title: WxuserGroupController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 2016年1月26日
 */
@Controller
@RequestMapping("/group/")
public class WxuserGroupController extends BaseController {

	@Inject
	private WxuserGroupMapper wxuserGroupMapper;

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/func/listWxuserGroup";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		WxuserGroupFormMap wxuserGroupFormMap = getFormMap(WxuserGroupFormMap.class);
		wxuserGroupFormMap = toFormMap(wxuserGroupFormMap, pageNow, pageSize, wxuserGroupFormMap.getStr("orderby"));
		pageView.setRecords(wxuserGroupMapper.findByPage(wxuserGroupFormMap));
		return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/func/addWxuserGroup";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@SystemLog(module = "功能管理", methods = "分组管理-新增分组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	public String addEntity() {
		try {
			WxuserGroupFormMap wxuserGroupFormMap = getFormMap(WxuserGroupFormMap.class);
			wxuserGroupMapper.addEntity(wxuserGroupFormMap);
		} catch (Exception e) {
			throw new SystemException("新增分组异常");
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "分组管理-删除分组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			List<WeixinUserFormMap> list = wxuserGroupMapper.findByAttribute("groupId", id, WeixinUserFormMap.class);
			if (CollectionUtils.isEmpty(list)) {
				wxuserGroupMapper.deleteByAttribute("id", id, WxuserGroupFormMap.class);
			} else {
				return "该分组下有关联用户，不允许删除";
			}
		}
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("wxuserGroup", wxuserGroupMapper.findbyFrist("id", id, WxuserGroupFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/func/editWxuserGroup";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "分组管理-修改分组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		WxuserGroupFormMap wxuserGroupFormMap = getFormMap(WxuserGroupFormMap.class);
		wxuserGroupMapper.editEntity(wxuserGroupFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("listAll")
	public List<WxuserGroupFormMap> listAll() throws Exception {
		return wxuserGroupMapper.findByWhere(new WxuserGroupFormMap());
	}
}
